package com.gwtgeo.ui.client.mvp.layers;

import java.util.Map;

import com.google.gwt.user.client.ui.HasText;
import com.gwtgeo.ui.client.mvp.adapter.HasTextFormItemAdapter;
import com.gwtgeo.ui.client.mvp.windowsmanager.WindowedDisplay;
import com.gwtgeo.ui.client.mvp.windowsmanager.WindowedView;
import com.smartgwt.client.types.HeaderControls;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.HasClickHandlers;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class AddLayerView extends WindowedView<VLayout> implements
		AddLayerPresenter.Display {

	private DynamicForm addLayerForm;

	private TextItem layerNameItem;

	private TextItem layerUrlItem;

	private TextItem layerParamsItem;

	private IButton acceptButton;

	private IButton cancelButton;

	public AddLayerView() {
		super();
	}

	@Override
	protected VLayout buildWidget() {

		VLayout widget = new VLayout();

		addLayerForm = new DynamicForm();

		layerNameItem = new TextItem("layerName", "Name");
		layerNameItem.setWidth(300);
		layerNameItem.setRequired(true);

		layerUrlItem = new TextItem("layerUrl", "URL");
		layerUrlItem.setWidth(300);
		layerUrlItem.setRequired(true);

		layerParamsItem = new TextItem("layerParams", "Params");
		layerParamsItem.setWidth(300);
		layerParamsItem.setRequired(true);

		addLayerForm.setItems(layerNameItem, layerUrlItem, layerParamsItem);

		addLayerForm.setWidth100();
		addLayerForm.setHeight100();

		widget.addMember(addLayerForm);

		HLayout buttonsPanel = new HLayout();

		acceptButton = new IButton("Accept");
		cancelButton = new IButton("Cancel");

		buttonsPanel.addMember(acceptButton);
		buttonsPanel.addMember(cancelButton);
		buttonsPanel.setMembersMargin(5);

		widget.addMember(buttonsPanel);

		return widget;
	}

	@Override
	public HasText getLayerName() {
		return new HasTextFormItemAdapter(this.layerNameItem);
	}

	@Override
	public HasText getLayerParams() {
		return new HasTextFormItemAdapter(this.layerParamsItem);
	}

	@Override
	public HasText getLayerUrl() {
		return new HasTextFormItemAdapter(this.layerUrlItem);
	}

	@Override
	public HasClickHandlers getAcceptClick() {
		return this.acceptButton;
	}

	@Override
	public HasClickHandlers getCancelClick() {
		return this.cancelButton;
	}

	@Override
	protected int getWindowHeight() {
		return 150;
	}

	@Override
	protected String getWindowTitle() {
		return "Add Layer";
	}

	@Override
	protected int getWindowWidth() {
		return 400;
	}

	@Override
	public boolean validate() {
		return addLayerForm.validate();
	}

	@Override
	protected void addHeaderControls(Map<String, HasClickHandlers> headers) {
		window.setHeaderControls(HeaderControls.HEADER_LABEL, headers
				.get(WindowedDisplay.CLOSE_HEADER));
	}
}
